package com.salary.entity.dto;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.salary.entity.BasicSalary;
import com.staff.entity.Staff;


public class BasicSalaryDTO {
	private Long id = null;
	private String staff;
	private String code;
	private String department;
	private String position;
	private float amount;
	private Date createTime;
	private Date updateTime;
	
	
	public BasicSalaryDTO(BasicSalary bs,Staff staff,String department,String position) {
		super();
		if(bs!=null) {
			this.id = bs.getId();
			this.amount = bs.getAmount();
			this.createTime = bs.getCreateTime();
			this.updateTime = bs.getUpdateTime();
		}
		
		if(staff!=null) {
			this.staff = staff.getStaffName();
			this.code = staff.getStaffNum();
			this.department = department;
			this.position = position;
		}
	}


	public BasicSalaryDTO() {
		super();
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public String getStaff() {
		return staff;
	}


	public void setStaff(String staff) {
		this.staff = staff;
	}


	public String getCode() {
		return code;
	}


	public void setCode(String code) {
		this.code = code;
	}


	public String getDepartment() {
		return department;
	}


	public void setDepartment(String department) {
		this.department = department;
	}


	public String getPosition() {
		return position;
	}


	public void setPosition(String position) {
		this.position = position;
	}


	public float getAmount() {
		return amount;
	}


	public void setAmount(float amount) {
		this.amount = amount;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getCreateTime() {
		return createTime;
	}


	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getUpdateTime() {
		return updateTime;
	}


	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}


	public static Specification<BasicSalary> getSpecification( final BasicSalaryDTO dto,Staff staff)
	{
		Specification<BasicSalary> spec = new Specification<BasicSalary>() {
			public Predicate toPredicate(Root<BasicSalary> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				
				 if(null != dto && !StringUtils.isEmpty(staff)) {
					 Predicate  p1 =  cb.equal(root.get("staff").as(Staff.class),staff);
					 list.add(p1);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
